package tech.aistar.clothesspringboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.aistar.clothesspringboot.pojo.result.ResultReturn;
import tech.aistar.pojo.User;
import tech.aistar.clothesspringboot.service.UserService;
import org.springframework.util.DigestUtils;



@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResultReturn loginByName(@RequestBody User user){
        log.info("使用用户名登录:{}",user);
        if (user == null ||
                user.getUsername() == null || user.getPassword() == null ||
                user.getUsername().isEmpty() || user.getPassword().isEmpty()) {
            return ResultReturn.fail("用户名或密码错误");///验证用户，用户名或密码不能为空
        }

        return userService.loginByName(user);
    }
     //添加用户
    @PostMapping("/register")
    public ResultReturn register(@RequestBody User user){
        log.info("添加用户:{}",user);
        if (user == null ||
                user.getUsername() == null || user.getPassword() == null ||
                user.getUsername().isEmpty() || user.getPassword().isEmpty()) {
            return ResultReturn.fail("用户名或密码错误");///验证用户，用户名或密码不能为空
        }
         int rows = userService.insertUser(user);
        if (rows > 0) {
            return ResultReturn.success("添加用户成功");
        }
        return ResultReturn.fail("添加用户失败");
    }

}
